<h2>Why is this an issue?</h2>
<p>Java virtual threads enable the JVM to optimize the use of OS threads by mounting and unmounting them as needed. This makes them more efficient
when dealing with blocking operations such as I/O or HTTP requests.</p>
<p>This rule applies only to code running on Java versions older than 24. Since Java 24, virtual threads are no longer pinned when executing
<code>synchronized</code> code.</p>
<p>For Java version 21 to 23, when code is executed inside a <code>synchronized</code> block or method, the virtual thread remains pinned to its
underlying OS thread and cannot be unmounted during a blocking operation. This causes the OS thread to be blocked, which can impact the scalability of
the application.</p>
<p>Therefore, in environments running a Java version below 24, virtual threads should not execute code that contains <code>synchronized</code> blocks
or invokes <code>synchronized</code> methods. Platform threads should be used in these cases instead.</p>
<p>This rule raises an issue when a virtual thread contains <code>synchronized</code> blocks or invokes <code>synchronized</code> methods.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
void enqueue(){
    Thread.startVirtualThread(() -&gt; { // Noncompliant; use a platform thread instead
            setupOperations();
            dequeLogic();
        }
    });
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
void enqueue(){
    new Thread(() -&gt; {
        synchronized {
            setupOperations();
            dequeLogic();
        }
    }).start();
}
</pre>
<h4>Noncompliant code example</h4>
<pre data-diff-id="2" data-diff-type="noncompliant">
void enqueue2(){
    Thread.startVirtualThread(() -&gt; { // Noncompliant; use a platform thread instead of a virtual one
        if(someCondition){
            synchronizedMethod();
        }else{
            defaultLogic();
        }
    });
}
synchronized void synchronizedMethod(){}
void defaultLogic(){}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="2" data-diff-type="compliant">
void enqueue2(){
    new Thread(() -&gt; {
        if(someCondition){
            synchronizedMethod();
        }else{
            defaultLogic();
        }
    }).start();
}
synchronized void synchronizedMethod(){}
void defaultLogic(){}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> Java Documentation - <a href="https://openjdk.org/jeps/444#:~:text=There%20are%20two,by%20capturing%20carriers">Virtual threads, pinning
  scenarios</a> </li>
  <li> Java Documentation - <a href="https://openjdk.org/jeps/491">Synchronize Virtual Threads without Pinning</a> </li>
</ul>

